﻿// ***********************************************************************
// Project          : Micua
// Assembly         : Micua.Utility
// Author           : Administrator
// Created          : 2014-01-05 11:27 AM
// 
// Last Modified By : Administrator
// Last Modified On : 2014-01-05 11:27 AM
// ***********************************************************************
// <copyright file="Convert.cs" company="Wedn.Net">
//     Copyright (c) Wedn.Net. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text.RegularExpressions;

namespace Micua.Utility
{
    /// <summary>
    /// 转换拓展方法
    /// </summary>
    public static partial class Convert
    {
        #region Convert string type to other types

        #region String to Enum +static T ToEnum<T>(this string s) where T : struct
        /// <summary>
        /// String to Enum<see cref="T"/>
        /// </summary>
        /// <remarks>
        ///  2013-11-18 18:53 Created By iceStone
        /// </remarks>
        /// <typeparam name="T">枚举类型</typeparam>
        /// <param name="s"></param>
        /// <returns></returns>
        public static T ToEnum<T>(this string s) where T : struct
        {
            T result;
            Enum.TryParse(s, true, out result);
            return result;
        }
        #endregion

        #region String to Slug +static string ToSlug(this string s)
        /// <summary>
        /// String to Slug
        /// </summary>
        /// <remarks>
        ///  2013-11-18 18:53 Created By iceStone
        /// </remarks>
        /// <param name="s">String</param>
        /// <returns>Slug</returns>
        public static string ToSlug(this string s)
        {
            return string.IsNullOrEmpty(s)
                ? string.Empty : Regex.Replace(s.Trim().ToLower(), @"\s+", "-");
        }
        #endregion

        #endregion

        #region Convert DateTime type to other types

        private const string DateFormat = "yyyy-MM-dd HH:ss";
        /// <summary>
        /// 时间转换成字符串
        /// </summary>
        /// <remarks>
        ///  2014-01-05 13:32:34 Created By iceStone
        /// </remarks>
        /// <param name="dateTime">时间</param>
        /// <returns>字符串</returns>
        public static string ToString(this DateTime dateTime)
        {
            return dateTime.ToString(DateFormat);
        }

        /// <summary>
        /// 将DateTime时间换成中文
        /// </summary>
        /// <remarks>
        ///  2013-11-18 18:53 Created By iceStone
        /// </remarks>
        /// <param name="dateTime">时间</param>
        /// <returns>System.String.</returns>
        /// <example>
        /// 2012-12-21 12:12:21.012 → 1月前
        /// 2011-12-21 12:12:21.012 → 1年前
        /// </example>
        public static string ToChsStr(this DateTime dateTime)
        {
            var ts = DateTime.Now - dateTime;
            if ((int)ts.TotalDays >= 365)
                return (int)ts.TotalDays / 365 + "年前";
            if ((int)ts.TotalDays >= 30 && ts.TotalDays <= 365)
                return (int)ts.TotalDays / 30 + "月前";
            if ((int)ts.TotalDays == 1)
                return "昨天";
            if ((int)ts.TotalDays == 2)
                return "前天";
            if ((int)ts.TotalDays >= 3 && ts.TotalDays <= 30)
                return (int)ts.TotalDays + "天前";
            if ((int)ts.TotalDays != 0) return dateTime.ToString("yyyy年MM月dd日");
            if ((int)ts.TotalHours != 0)
                return (int)ts.TotalHours + "小时前";
            if ((int)ts.TotalMinutes == 0)
                return "刚刚";
            return (int)ts.TotalMinutes + "分钟前";
        }

        #endregion

        #region 匿名类转换成字典 +static IDictionary<string, object> ToDictionary(this object obj, char attrSeparator)
        /// <summary>
        /// 匿名类转换成字典
        /// </summary>
        /// <remarks>
        ///  2013-11-18 18:53 Created By iceStone
        /// </remarks>
        /// <param name="obj">匿名类</param>
        /// <param name="attrSeparator">属性分隔符，类属性不能有其他字符，_代表字符</param>
        /// <example>
        /// new {id="example",data_something="something"} → {{"id","example"},{"data_something","something"}}
        /// </example>
        /// <returns>属性字典</returns>
        public static IDictionary<string, object> ToDictionary(this object obj, char attrSeparator = '_')
        {
            var dictionary = new Dictionary<string, object>();
            if (obj != null)
            {
                foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(obj))
                {
                    if (attrSeparator == '_')
                        dictionary.Add(descriptor.Name, descriptor.GetValue(obj));
                    else
                        dictionary.Add(descriptor.Name.Replace('_', attrSeparator), descriptor.GetValue(obj));
                }
            }
            return dictionary;
        }
        #endregion
    }
}